const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatTimeB = timestamp => {
  const date = new Date(timestamp * 1000)
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatTimeC = (timestamp, aim_timestamp) => {
  let publishTime = aim_timestamp,
    d_seconds,
    d_minutes,
    d_hours,
    d_days,
    timeNow = timestamp,
    d,
    date = new Date(publishTime * 1000),
    Y = date.getFullYear(),
    M = date.getMonth() + 1,
    D = date.getDate(),
    H = date.getHours(),
    m = date.getMinutes(),
    s = date.getSeconds();
  //小于10的在前面补0
  if (M < 10) {
    M = '0' + M;
  }
  if (D < 10) {
    D = '0' + D;
  }
  if (H < 10) {
    H = '0' + H;
  }
  if (m < 10) {
    m = '0' + m;
  }
  if (s < 10) {
    s = '0' + s;
  }

  d = timeNow - publishTime;
  d_days = parseInt(d / 86400);
  d_hours = parseInt(d / 3600);
  d_minutes = parseInt(d / 60);
  d_seconds = parseInt(d);

  if (d_days > 0 && d_days < 3) {
    return {
      type: 0,
      time: d_days + '天前'
    };
  } else if (d_days <= 0 && d_hours > 0) {
    return {
      type: 0,
      time: d_hours + '小时前'
    };
  } else if (d_hours <= 0 && d_minutes > 0) {
    return {
      type: 0,
      time: d_minutes + '分钟前'
    };
  } else if (d_seconds < 60) {
    if (d_seconds <= 0) {
      return {
        type: 0,
        time: '刚刚'
      };
    } else {
      return {
        type: 0,
        time: d_seconds + '秒前'
      };
    }
  } else if (d_days >= 3 && d_days < 30) {
    return {
      type: 1,
      year: Y + '/' + M + '/' + D,
      time: H + ':' + m
    };
  }
  else if (d_days >= 30) {
    return {
      type: 1,
      year: Y + '/' + M + '/' + D,
      time: H + ':' + m
    };
  }
}

const getTimeSeconds = timestamp => {
  const date = new Date(timestamp)
  const second = date.getSeconds()

  return second
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
const imestampToTime = (timestamp)=> {
  var date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
  var Y = date.getFullYear() + '/';
  var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '/';
  var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate() + "";
  var h = date.getHours() + ':';
  var m = date.getMinutes();
  var s = date.getSeconds();
  if (String(m).length == 1) {
    m = "0" + m;
  }
  if (String(s).length == 1){
    s ="0"+ s;
  }
  // var s = date.getSeconds();
  // return  M + D + " " + h + m + s;
  return  M + D + " " + h + m + ':' + s;
}//转化后格式xxxx-xx-xx,

module.exports = {
  formatTime: formatTime,
  formatTimeB: formatTimeB,
  formatTimeC: formatTimeC,
  getTimeSeconds: getTimeSeconds,
  imestampToTime: imestampToTime
}